package source;

/*
This file is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2, 1991 as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

A copy of the GNU General Public License can be found at:
http://www.gnu.org/licenses/gpl.html
*/
import java.io.*;

public class ItemHandler {
	public static int DropItemCount = 0;
	public static int MaxDropItems = 100000;
							//process() is called evry 500 ms
	public static int MaxDropShowDelay = 120; //120 * 500 = 60000 / 1000 = 60s
	public static int SDID = 90; //90 * 500 = 45000 / 1000 = 45s
					//SDID = Standard Drop Items Delay
	public static int[] DroppedItemsID = new int[MaxDropItems];
	public static int[] DroppedItemsX = new int[MaxDropItems];
	public static int[] DroppedItemsY = new int[MaxDropItems];
	public static int[] DroppedItemsN = new int[MaxDropItems];
	public static int[] DroppedItemsH = new int[MaxDropItems];
	public static int[] DroppedItemsDDelay = new int[MaxDropItems];
	public static int[] DroppedItemsSDelay = new int[MaxDropItems];
	public static int[] DroppedItemsDropper = new int[MaxDropItems];
	public static int[] DroppedItemsDeletecount = new int[MaxDropItems];
	public static boolean[] DroppedItemsAlwaysDrop = new boolean[MaxDropItems];

	static {
		for(int i = 0; i < MaxDropItems; i++) {
			ResetItem(i);
		}
		loadDrops("config\\drops.cfg");
	}

	public static void process() {
		for(int i = 0; i < MaxDropItems; i++) {
			if (DroppedItemsID[i] > -1) {
				if (DroppedItemsDDelay[i] > -10) {
					DroppedItemsDDelay[i]--;
				}
				if (DroppedItemsSDelay[i] < (MaxDropShowDelay + 10)) {
					DroppedItemsSDelay[i]++;
				}
				if (DroppedItemsSDelay[i] >= MaxDropShowDelay && DroppedItemsAlwaysDrop[i] == false) {
					for (int j = 1; j < PlayerHandler.maxPlayers; j++) {
						if (PlayerHandler.players[j] != null) {
							PlayerHandler.players[j].MustDelete[i] = true;
						}
					}
				}
			}
		}
	}

	public static void ResetItem(int ArrayID) {
		DroppedItemsID[ArrayID] = -1;
		DroppedItemsX[ArrayID] = -1;
		DroppedItemsY[ArrayID] = -1;
		DroppedItemsN[ArrayID] = -1;
		DroppedItemsH[ArrayID] = -1;
		DroppedItemsDDelay[ArrayID] = -1;
		DroppedItemsSDelay[ArrayID] = 0;
		DroppedItemsDropper[ArrayID] = -1;
		DroppedItemsDeletecount[ArrayID] = 0;
		DroppedItemsAlwaysDrop[ArrayID] = false;
	}

	public static boolean loadDrops(String FileName) {
		String line = "";
		String token = "";
		String token2 = "";
		String token2_2 = "";
		String[] token3 = new String[10];
		boolean EndOfFile = false;
		int ReadMode = 0;
		BufferedReader characterfile = null;
		try {
			characterfile = new BufferedReader(new FileReader("./"+FileName));
		} catch(FileNotFoundException fileex) {
			misc.println(FileName+": file not found.");
			return false;
		}
		try {
			line = characterfile.readLine();
		} catch(IOException ioexception) {
			misc.println(FileName+": error loading file.");
			return false;
		}
		while(EndOfFile == false && line != null) {
			line = line.trim();
			int spot = line.indexOf("=");
			if (spot > -1) {
				token = line.substring(0, spot);
				token = token.trim();
				token2 = line.substring(spot + 1);
				token2 = token2.trim();
				token2_2 = token2.replaceAll("\t\t", "\t");
				while (token2_2.contains("\t\t")){
					token2_2 = token2_2.replaceAll("\t\t", "\t");
				}
				token3 = token2_2.split("\t");
				if (token.equals("drop")) {
					DroppedItemsID[DropItemCount] = Integer.parseInt(token3[0]);
					DroppedItemsX[DropItemCount] = Integer.parseInt(token3[1]);
					DroppedItemsY[DropItemCount] = Integer.parseInt(token3[2]);
					DroppedItemsN[DropItemCount] = Integer.parseInt(token3[3]);
					DroppedItemsH[DropItemCount] = Integer.parseInt(token3[4]);
					DroppedItemsAlwaysDrop[DropItemCount] = true;
					DropItemCount++;
				}
			} else {
				if (line.equals("[ENDOFDROPLIST]")) {
					try { characterfile.close(); } catch(IOException ioexception) { }
					return true;
				}
			}
			try {
				line = characterfile.readLine();
			} catch(IOException ioexception1) { EndOfFile = true; }
		}
		try { characterfile.close(); } catch(IOException ioexception) { }
		return false;
	}
}
